package bank.support;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author Mathew Lowery
 * @version 1.0
 */

public class BankAccount {
  /**
   * Account number for this BankAccount object.  (See uniqueAccountNumber).
   */
  private int accountNumber;

  /**
   * Name of the customer associated with this account.
   */
  private String customerName;

  /**
   * Current balance of this account.  BankAccounts should never have negative
   * balances.  (See withdraw method).
   */
  private double balance;

  /**
   * Transactions history starting from account creation.
   * transactions[transactionCount-1] always refers the latest transaction.
   */
  private double[] transactions = new double[20];

  /**
   * Number of transactions associated with this account.
   */
  private int transactionCount;

  /**
   * All BankAccount objects use this number to generate a unique bank account
   * number.
   */
  private static int uniqueAccountNumber;

  /**
   * Constructor that takes customer name and opening balance.  Opening balance
   * becomes the first transaction (deposit) for this account.  An account
   * number is also generated at this point.
   */
  public BankAccount(String customerName, double openingBalance) {
    this.customerName = customerName;
    accountNumber = uniqueAccountNumber++;
    balance = openingBalance;
    transactions[transactionCount++] = openingBalance;
  }

  /**
   * Returns the account number of this bank account.
   */
  public int getAccountNumber() {
    return accountNumber;
  }

  /**
   * Returns String representing bank account number, customer name, and
   * balance.
   */
  public String getAccountInfo() {
    return "AccountNum: " + accountNumber + ", Name: " + customerName +
        ", Balance: " + balance;
  }

  /**
   * Returns String representing last N transactions on this account.  If there
   * is not lastNTransactions in the transaction history, print all
   * transactions.  Transactions appear in the order in which they occurred.
   */
  public String getTransactionInfo(int lastNTransactions) {
    StringBuffer buf = new StringBuffer();
    buf.append("Last " + lastNTransactions + " transactions: ");
    int startingIndex = Math.max(0, transactionCount - lastNTransactions);
    for (int i = startingIndex; i < transactionCount; i++) {
      if (i != startingIndex) {buf.append(", ");}
      buf.append(transactions[i]);
    }
    return buf.toString();
  }

  /**
   * Reduces the balance of this account by amount.  If doing so will take the
   * balance negative, do not withdraw the amount, and print a message to the
   * screen stating the unsuccessful completion of this operation.  Withdraw
   * amount is entered into the transaction history as a negative amount.
   */
  public void withdraw(double amount) {
    if (amount > balance) {
      System.out.println("Cannot complete withdraw operation:  "
                         + "insufficient funds.");
      return;
    }
    balance -= amount;
    transactions[transactionCount++] = -1 * amount;
  }

  /**
   * Increases the balance of this account by amount.  Deposit amount is entered
   * into the transaction history as a positive amount.
   */
  public void deposit(double amount) {
    balance += amount;
    transactions[transactionCount++] = amount;
  }
} // BankAccount